using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Softelvdm.SftTabsNET;
using Softelvdm.Controls;

namespace CloseSample1 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        Image imgClose = Bitmap.FromFile("..\\..\\Close1_16x16.png");
        Image imgCloseHover = Bitmap.FromFile("..\\..\\Close1Hover_16x16.png");
        Image imgNew = Bitmap.FromFile("..\\..\\NewTab1_16x16.png");

        private void Form1_Load(object sender, EventArgs e) {

            // This sample demonstrates how to use a "New Tab" and close buttons in each tab.
            // The "close button" isn't really a button, instead we use an image.
            // To prepare for this sample, create a new project with a blank form and add
            // a SftTabs/NET control named sftTabs1.
            // In addition, adjust the above FromFile methods to use a (small) bitmap
            // that is located on your system.

            sftTabs1.Initializing = true;

            sftTabs1.FocusHighlightStyle = FocusHighlightStyleEnum.None;

            sftTabs1.Scrolling.Style = ScrollingStyle.AlwaysLeft;
            InsertTab("Tab &1", sftTabs1.TabCollection.Count);
            InsertTab("Tab &2", sftTabs1.TabCollection.Count);

            // Add "New" tab
            TabClass tb = sftTabs1.TabCollection.Add();
            tb.ToolTip = "Add a new tab";
            tb.Image = imgNew;
            tb.ImagePart.VisibleAppearance = VisibleAppearanceEnum.OwnerHot;
            tb.ImagePart.PartAlignment = PartAlignmentEnum.Center;

            // Make the first tab active
            sftTabs1.Current = 0;
            // Update button images
            UpdateCloseButtonImages();

            sftTabs1.Switched += new SftTabs.SwitchedEventHandler(sftTabs1_Switched);

            sftTabs1.Initializing = false;
        }

        // we have just switched to a new tab
        void sftTabs1_Switched(object sender, EventArgs e) {
            if (sftTabs1.Current == sftTabs1.Count-1) {
                // We switched to the last tab, which is the "New Tab" tab. Insert a new tab
                TabClass tbNew = InsertTab("New&" + sftTabs1.Count.ToString(), sftTabs1.Current);
                sftTabs1.Current = tbNew.Index; // Make it the current tab
                UpdateCloseButtonImages();
            }
        }

        // Insert a new tab
        private TabClass InsertTab(string strLabel, int index) {
            TabClass tb = sftTabs1.TabCollection.Insert(index);
            tb.Text = strLabel;
            tb.ToolTip = strLabel.Replace("&","");
            tb.TextPart.PartAlignment = PartAlignmentEnum.Center;
            tb.TextPart.HAlign = HAlignmentOptionalEnum.Center;
            tb.Image = imgClose;
            tb.ImagePart.HoverImage = imgCloseHover;
            tb.ImagePart.PartAlignment = PartAlignmentEnum.Center;
            tb.ImagePart.VisibleAppearance = VisibleAppearanceEnum.OwnerSelectedOrHide;
            tb.ImagePart.Action += new GenericPartClass.ActionEventHandler(ImagePart_Action);
            return tb;
        }

        // The close image of a tab was clicked
        void ImagePart_Action(object sender, ActionEventArgs e) {
            TabClass tb = (TabClass) e.Part.PartOwner;
            int index = tb.Index; 
            sftTabs1.TabCollection.RemoveAt(index);
            --index;
            if (index >= 0)
                sftTabs1.Current = index;
            UpdateCloseButtonImages();
        }

        // We have to make sure that if there is only one tab, we can't close
        // that tab. We do that by simply hiding the close button images
        void UpdateCloseButtonImages()
        {
            // If we have more than 2 tabs, we don't need to hide the close button images
            bool hide = sftTabs1.Count <= 2;
            foreach (TabClass tb in sftTabs1.TabCollection) {
                if (tb.ImagePart.Image == imgClose) { // this is a close button image
                    if (hide)
                        tb.ImagePart.VisibleAppearance = VisibleAppearanceEnum.Never;
                    else
                        tb.ImagePart.VisibleAppearance = VisibleAppearanceEnum.OwnerSelectedOrHide;
                }
            }
        }        
    }
}